//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Xml;
using CommunityServer.Components;

namespace CommunityServer.Discussions.Components
{
	/// <summary>
	/// Summary description for AutoApproveModule.
	/// </summary>
	public class AutoApproveForumModule : ICSModule
	{
		public AutoApproveForumModule()
		{
		}

		public void Init(CSApplication csa, XmlNode node)
		{
			csa.PrePostUpdate += new CSPostEventHandler(csa_BeforeProcessPost);
		}

		private void csa_BeforeProcessPost(IContent content, CSPostEventArgs e)
		{
			// Check if this is the creation of a new forum post
			ForumPost fp = content as ForumPost;
			if ((fp == null) || (e.State != ObjectState.Create) || (e.ApplicationType != ApplicationType.Forum))
				return;

			// Auto-approve all posts from admins and moderators
			if (fp.User.IsModerator || fp.User.IsForumAdministrator || fp.User.IsAdministrator)
				fp.AutoApprove = true;
		}
	}
}
